package com.cloud.phone.vpn;

import com.blankj.utilcode.util.LogUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;

import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

/**
 * @auth: njb
 * @date: 2024/5/9 11:15
 * @desc: OKHttp使用代理
 */
public class OKHttpProxy {
    private static final String TAG = "OkHttpProxy";
    String proxyHost;
    int proxyPort;
    String proxyAccount;
    String proxyPwd;

    /**
     * @param host 代理主机地址
     * @param port 代理主机端口
     */
    public static OKHttpProxy build(String host, int port) {
        OKHttpProxy proxy = new OKHttpProxy();
        proxy.proxyHost = host;
        proxy.proxyPort = port;
        return proxy;
    }

    /**
     * @param host 代理主机地址
     * @param port 代理主机端口
     * @param acc 代理认证账号
     * @param pwd 代理认证口令
     */
    public static OKHttpProxy build(String host, int port, String acc, String pwd) {
        OKHttpProxy proxy = new OKHttpProxy();
        proxy.proxyHost = host;
        proxy.proxyPort = port;
        proxy.proxyAccount = acc;
        proxy.proxyPwd = pwd;
        return proxy;
    }

    public void test() {
        String targetUrl = "http://myip.ipip.net";
        OkHttpClient client;
        if (proxyAccount == null || proxyPwd == null) {
            client = getHttpClient(proxyHost, proxyPort);
        } else {
            // 账号密码验证
            client = getHttpClient(proxyHost, proxyPort, proxyAccount, proxyPwd);
        }

        Request request = new Request.Builder()
                .url(targetUrl)
                .build();
        try {
            Response response = client.newCall(request).execute();
            LogUtils.e(TAG,"===获取ip代理成功==="+(response.body() != null ? response.body().string() : null));
        }catch (Exception e){
            LogUtils.e(TAG,"===获取ip代理出错==="+e.getMessage());
            e.printStackTrace();
        }
    }

    /**
     * 代理不需要账号密码认证的httpClient
     */
    private static OkHttpClient getHttpClient(String proxyHost, int proxyPort) {
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
        return new OkHttpClient.Builder()
                .proxy(proxy)
                .build();
    }

    /**
     * 代理需要账号密码认证的httpClient
     */
    private static OkHttpClient getHttpClient(String proxyHost, int proxyPort, String acc, String pwd) {
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
        // 账号密码验证
        Authenticator authenticator = new Authenticator() {
            @Override
            public Request authenticate(Route route, Response resp) throws IOException {
                String credential = Credentials.basic(acc, pwd);
                return resp.request().newBuilder().header("Proxy-Authorization", credential).build();
            }
        };
        return new OkHttpClient.Builder()
                .proxy(proxy)
                .proxyAuthenticator(authenticator)
                .build();
    }
}
